package com.blb.dao.user.impl;

import com.blb.dao.user.IndexDao;
import com.blb.pojo.Carousel;
import com.blb.pojo.Product;
import com.blb.pojo.ProductType;
import com.blb.utils.JDBCUtils;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class IndexDaoImpl implements IndexDao {


    @Override
    public List<Product> selectNewProduct() throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql ="select * from t_product order by create_time desc limit 0,6 ";
        List<Product> list = JDBCUtils.getList(connection, Product.class,sql);
        connection.close();
        return list;
    }

    public List<Product> selectProduct() throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql ="select * from t_product limit 0,6 ";
        List<Product> list = JDBCUtils.getList(connection, Product.class,sql);
        connection.close();
        return list;
    }

    @Override
    public List selectProductType() throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from t_product_type";
        List<ProductType> list = JDBCUtils.getList(connection, ProductType.class, sql);
        connection.close();
        return list;
    }

    @Override
    public List<ProductType> getProductTypeByName(String name,String size) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql ="select * from t_product_type where name = ? limit ?";
        List<ProductType> list = JDBCUtils.getList(connection, ProductType.class,sql,name,size);
        connection.close();
        return list;
    }

    @Override
    public void insertOrderData(int id, int productId, int num) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "insert into t_shop_cart_product(user_id,product_id,num) values(?,?,?,?)";
        JDBCUtils.prepareStatement(connection,sql,id,productId,num).executeUpdate();
        connection.close();
    }
}
